package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.list.feign.SearchFeign;
import com.atguigu.gmall.web.util.Page;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
@RequestMapping("/api/search")
public class SearchController {
    @Autowired
    private SearchFeign searchFeign;
    @GetMapping
    public String search(Model model,@RequestParam Map<String,String> searchData){
        //调用List微服务的搜索接口
        Map<String, Object> searchResult = searchFeign.search(searchData);
        //请求参数返回
        model.addAttribute("searchData",searchData);
        //获取请求的上下文
        String url = getUrl(searchData);
        model.addAttribute("url",url);
        String sortUrl = getSortUrl(searchData);
        model.addAttribute("sortUrl",sortUrl);
        //获取总记录数
        Object totalHits = searchResult.get("totalHits");
        //获取当前页码
        Integer pageNum = getPage(searchData.get("pageNum"));
        Page<Object> pageInfo
                = new Page<>(Long.valueOf(totalHits.toString()), pageNum, 100);
        model.addAttribute("pageInfo",pageInfo);
        //将搜索结果返回
        model.addAllAttributes(searchResult);

        return "list";
    }

    /**
     * 获取请求地址的参数
     * @param searchData
     * @return
     */
    private String getUrl(Map<String, String> searchData) {
        StringBuffer sb = new StringBuffer("/api/search?");
        searchData.entrySet().stream().forEach(param ->{
            String key = param.getKey();
            if (!key.equals("pageNum")){
                String value = param.getValue();
                sb.append(key).append("=").append(value).append("&");
            }
        });
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    /**
     * 拼接排序的URL 因为排序的URL不用保存上一次的
     * @param searchData
     * @return
     */
    private String getSortUrl(Map<String, String> searchData) {
        //初始化
        StringBuffer sb = new StringBuffer("/api/search?");

        searchData.entrySet().stream().forEach(param ->{
            String key = param.getKey();
            if (!key.equals("sortField")&&
                 !key.equals("sortRule")&&
                 !key.equals("pageNum")){
                String value = param.getValue();
                sb.append(key).append("=").append(value).append("&");
            }
        });
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    /**
     * 计算页码
     * @param pageNum
     * @return
     */
   private Integer getPage(String pageNum){
        try {
            //计算页码
            int i = Integer.parseInt(pageNum);
            if (i <1 || i >= 200){
                return 1;
            }
            return i;
        }catch (Exception e){
            return 1;
        }
   }
}
